package com.zsummer.zero.quartz;

import java.util.concurrent.TimeUnit;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * quartz定时任务
 * 
 * @author zhouyujiang
 *
 */
public class Demo1 {

	public static void main(String[] args) {
		
		run1();
	}

	private static void run1() {
		JobDetail jobDetail = JobBuilder.newJob(AJob.class)
				.withIdentity("aJob")
				.build();
		
		// withIdentity 属性
		// startNow 立即启动
		// withSchedule 设置计划
		// usingJobData 给具体的job传递参数
//		Trigger trigger = TriggerBuilder.newTrigger()
//				.withIdentity("aTrigger")
//				.startNow()
//				.withSchedule(SimpleScheduleBuilder.simpleSchedule()
//						.withIntervalInSeconds(1)
//						.repeatForever())
//				.build();
		Trigger trigger = TriggerBuilder.newTrigger()
		.withIdentity("aTrigger")
		.withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?"))
		.build();
		
		Scheduler scheduler = null;
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.scheduleJob(jobDetail, trigger);
			scheduler.start();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			TimeUnit.SECONDS.sleep(20);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			scheduler.shutdown();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
